<template>
	<view>
		<tabs @setSwipterData="setSwipterData" :tabList="tabItems"></tabs>
		<view v-if="integralInfo.total > 0" class="integral">
			<block v-for="(item, index) in integralInfo.records" :key="index">
				<view class="integral-item">
					<view class="integral-item-head">{{item.origin}}</view>
					<view class="integral-item-fooder">
						<view>时间：{{item.gmtModified}}</view>
						<view class="integral-item-fooder-value">积分：{{item.category == 1 ? '+' : '-'}}{{item.value}}</view>
					</view>
				</view>
			</block>
		</view>
		<view v-if="integralInfo.total <= 0" class="integral-empty">
			数据不存在
		</view>
	</view>
</template>

<script>
	// pages/ucenter/integral/integral.js
	let app = getApp();
	import tabs from "../../../components/tabs/tabs";

	export default {
		data() {
			return {
				tabItems: [
					["消耗记录", "", true],
					["来源记录", "", false],
					["获取积分", "", false]
				],
				integralInfo: {},
				current: 1,
				filterData: {
					orgin: "",
					category: "0"
				},
				userInfo: {}
			};
		},

		components: {
			tabs
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			this.getUsuerinfo();
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			let filterData = this.filterData;
			this.setData({
				current: 1
			});
			this.getIntegral(this.current, filterData.category, filterData.origin);
			uni.stopPullDownRefresh();
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			let current = this.current;

			if (current < this.integralInfo.pages) {
				let filterData = this.filterData;
				this.setData({
					current: current + 1
				});
				this.getIntegral(current + 1, filterData.category, filterData.origin);
			}
		},

		onShareAppMessage() {},

		methods: {
			setSwipterData(e) {
				const index = e.detail;
				let filterData = this.filterData;

				if (index == 0) {
					this.setData({
						current: 1
					});
					filterData.category = "0";
					this.getIntegral(this.current, filterData.category, filterData.origin);
					this.setData({
						filterData
					});
				}

				if (index == 1) {
					this.setData({
						current: 1
					});
					filterData.category = "";
					filterData.origin = "";
					this.getIntegral(this.current, filterData.category, filterData.origin);
					this.setData({
						filterData
					});
				}

				if (index == 2) {
					this.setData({
						current: 1
					});
					filterData.category = "1";
					this.getIntegral(this.current, filterData.category, filterData.origin);
					this.setData({
						filterData
					});

				}
			},

			getIntegral: function(current = 1, category = '0', origin = '') {
				let _this = this;

				let user = _this.userInfo;
				uni.showLoading({
					title: '加载中'
				});
				uni.request({
					url: `${app.globalData.baseUrl}/integral/selectAll/${current}?category=${category}&origin=${origin}&user=${user.id}`,
					complete: res => {
						uni.hideLoading();
					},
					fail: res => {
						uni.showToast({
							title: '加载失败',
							icon: "none"
						});
					},
					method: "GET",
					success: result => {
						let integral = result.data.dataMap.data;

						if (_this.current > 1) {
							// if (_this.data.current < _this.data.integralInfo.current)
							let integralInfo = _this.integralInfo;
							integral.records.forEach(item => {
								integralInfo.records.push(item);
							});

							_this.setData({
								integralInfo
							});
						} else {
							_this.setData({
								integralInfo: integral
							});
						}
					}
				});
			},
			getUsuerinfo: function() {
				let openid = uni.getStorageSync('openid');

				let _this = this;

				if (!openid) {
					uni.navigateBack({
						complete: res => {
							uni.showToast({
								title: '请登录',
								icon: "none"
							});
						}
					});
				} else {
					uni.request({
						url: `${app.globalData.baseUrl}/user/selectIsUser/${openid}`,
						complete: res => {},
						success: result => {
							_this.setData({
								userInfo: result.data.dataMap.data
							});

							let filterData = _this.filterData;
							filterData.category = "0";

							_this.getIntegral(this.current, filterData.category, filterData.origin);
						}
					});
				}
			},

			onShareTimeline() {}

		}
	};
</script>
<style>
	/* pages/ucenter/integral/integral.wxss */
	page {
		background: #efeff5;
	}

	.integral {
		width: 96%;
		margin: 2%;
	}

	.integral-item {
		background: white;
		margin-top: 10rpx;
		padding: 20rpx 10rpx;
	}

	.integral-item-head {
		font-size: large;
	}

	.integral-item-fooder {
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
	}

	.integral-item-fooder-value {
		color: red;
	}

	.integral-empty {
		margin-top: 20rpx;
		width: 96%;
		text-align: center;
	}
</style>
